package com.decent.manager.system.service.impl;

import com.decent.common.bo.MessageBean;
import com.decent.common.enums.ErrorCodeEnum;
import com.decent.common.enums.manager.BannerTypeEnum;
import com.decent.common.exception.ErrorCodeException;
import com.decent.common.oss.enums.OssFolderEnum;
import com.decent.common.oss.util.OssUtil;
import com.decent.manager.config.oss.OssConfiguration;
import com.decent.manager.system.dao.SystemBannerDao;
import com.decent.manager.system.dto.SystemBannerDTO;
import com.decent.manager.system.service.SystemBannerService;
import com.decent.manager.system.vo.SystemBannerVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import reactor.util.function.Tuple2;

import java.util.List;
import java.util.Objects;
import java.util.UUID;

/**
 * banner操作--service层
 * @author: decent Gjh
 * @date: 2022/10/24/15:43
 * @description:
 */
@Slf4j
@Service
public class SystemBannerServiceImpl implements SystemBannerService {
    private final ApplicationContext applicationContext;
    private final SystemBannerDao bannerDao;

    public SystemBannerServiceImpl(SystemBannerDao bannerDao, ApplicationContext applicationContext) {
        this.bannerDao = bannerDao;
        this.applicationContext = applicationContext;
    }

    /**
     * 新建banner
     * @param banner 信息
     * @return MessageBean 返回信息
     */
    @Override
    public MessageBean insertBanner(SystemBannerDTO banner) {
        // oss部分
        MultipartFile file = banner.getFile();
        String fileName = file.getOriginalFilename();
        String type = "";
        if (Objects.nonNull(fileName)){
            type = Objects.requireNonNull(file.getOriginalFilename()).substring(fileName.lastIndexOf("."));
        }
        Tuple2<String, String> logo = OssUtil.submitFile(file, OssFolderEnum.BANNER, UUID.randomUUID()+type, true);
        banner.setImageUrl(logo.getT2());
        //如果非url类型链接  则必须可以转换成数字
        if (banner.getBannerType() != BannerTypeEnum.URL){
            try {
                Integer.parseInt(banner.getValue());
            }catch (NumberFormatException e) {
                throw new ErrorCodeException(ErrorCodeEnum.INVALID_PARAMS,"参数value需是可转为数字的字符串");
            }
        }
        bannerDao.insertImage(banner);
        return new MessageBean(ErrorCodeEnum.OK,"添加banner成功");
    }

    /**
     * 更新banner
     * @param banner banner信息
     * @return MessageBean 返回信息
     */
    @Override
    public MessageBean updateBanner(SystemBannerDTO banner) {
        //如果非url类型链接  则必须可以转换成数字
        if (banner.getBannerType() != BannerTypeEnum.URL){
            try {
                Integer.parseInt(banner.getValue());
            }catch (NumberFormatException e) {
                throw new ErrorCodeException(ErrorCodeEnum.INVALID_PARAMS,"参数value需是可转为数字的字符串");
            }
        }
        int rowNum = bannerDao.updateBanner(banner);
        return rowNum == 0 ? new MessageBean(ErrorCodeEnum.DATA_NOT_FOUND,"修改失败,数据不存在请检查id") :
                new MessageBean(ErrorCodeEnum.OK,"修改成功");
    }

    /**
     * 删除banner
     * @param bannerId  要删除的Id值
     * @return  MessageBean 返回信息
     */
    @Override
    public MessageBean deleteBanner(int bannerId) {
        // TODO oss文件删除需返回知否成功 成功才可进行下一步(需修改模板核心代码)
        SystemBannerVO tempBanner = bannerDao.getBannerById(bannerId);
        if (tempBanner == null) {
            return new MessageBean(ErrorCodeEnum.DATA_NOT_FOUND,"图片不存在");
        }
        String ossPath = tempBanner.getImageUrl().substring(
                applicationContext.getBean("OssConfig",OssConfiguration.class).getPublicEndPoint().length()+1);
        OssUtil.deleteFile(ossPath);
        int rowNum = bannerDao.deleteBanner(bannerId);
        return rowNum == 0 ? new MessageBean(ErrorCodeEnum.DATA_NOT_FOUND,"删除失败") :
                new MessageBean(ErrorCodeEnum.OK,"删除成功");
    }

    /**
     * 获取banner
     * @return 含有banner列表的MessageBean
     */
    @Override
    public MessageBean getBanner() {
        List<SystemBannerVO> banners = bannerDao.getBannerList();
        return (banners == null || banners.isEmpty()) ? new MessageBean(ErrorCodeEnum.DATA_NOT_FOUND,"暂无banner"):
                new MessageBean(ErrorCodeEnum.DATA_NOT_FOUND,banners,"获取banner成功");
    }

}
